package com.example.hello.common;

import lombok.Data;

/**
 * 统一响应结果类
 */
@Data
public class Result<T> {
    /**
     * 响应码，1代表成功，0代表失败
     */
    private Integer code;
    
    /**
     * 提示信息
     */
    private String msg;
    
    /**
     * 返回的数据
     */
    private T data;
    
    /**
     * 成功响应
     */
    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.code = 1;
        result.msg = "success";
        return result;
    }
    
    /**
     * 成功响应，带数据
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.code = 1;
        result.msg = "success";
        result.data = data;
        return result;
    }
    
    /**
     * 失败响应
     */
    public static <T> Result<T> error(String msg) {
        Result<T> result = new Result<>();
        result.code = 0;
        result.msg = msg;
        return result;
    }
    
    /**
     * 失败响应，带数据
     */
    public static <T> Result<T> error(String msg, T data) {
        Result<T> result = new Result<>();
        result.code = 0;
        result.msg = msg;
        result.data = data;
        return result;
    }
}
